package com.ruoyi.settled.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.settled.domain.SettlementRecord;
import com.ruoyi.settled.service.ISettlementRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 结算记录Controller
 * 
 * @author huangchen
 * @date 2025-01-05
 */
@RestController
@RequestMapping("/record/record")
public class SettlementRecordController extends BaseController
{
    @Autowired
    private ISettlementRecordService settlementRecordService;

    /**
     * 查询结算记录列表
     */
//    @PreAuthorize("@ss.hasPermi('record:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(SettlementRecord settlementRecord)
    {
        startPage();
        List<SettlementRecord> list = settlementRecordService.selectSettlementRecordList(settlementRecord);
        return getDataTable(list);
    }

    /**
     * 导出结算记录列表
     */
//    @PreAuthorize("@ss.hasPermi('record:record:export')")
    @Log(title = "结算记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SettlementRecord settlementRecord)
    {
        List<SettlementRecord> list = settlementRecordService.selectSettlementRecordList(settlementRecord);
        ExcelUtil<SettlementRecord> util = new ExcelUtil<SettlementRecord>(SettlementRecord.class);
        util.exportExcel(response, list, "结算记录数据");
    }

    /**
     * 获取结算记录详细信息
     */
//    @PreAuthorize("@ss.hasPermi('record:record:query')")
    @GetMapping(value = "/{settlementCode}")
    public AjaxResult getInfo(@PathVariable("settlementCode") String settlementCode)
    {
        return success(settlementRecordService.selectSettlementRecordBySettlementCode(settlementCode));
    }

    /**
     * 新增结算记录
     */
//    @PreAuthorize("@ss.hasPermi('record:record:add')")
    @Log(title = "结算记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SettlementRecord settlementRecord)
    {
        return toAjax(settlementRecordService.insertSettlementRecord(settlementRecord));
    }

    /**
     * 修改结算记录
     */
//    @PreAuthorize("@ss.hasPermi('record:record:edit')")
    @Log(title = "结算记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SettlementRecord settlementRecord)
    {
        return toAjax(settlementRecordService.updateSettlementRecord(settlementRecord));
    }

    /**
     * 删除结算记录
     */
    @PreAuthorize("@ss.hasPermi('record:record:remove')")
    @Log(title = "结算记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{settlementCodes}")
    public AjaxResult remove(@PathVariable String[] settlementCodes)
    {
        return toAjax(settlementRecordService.deleteSettlementRecordBySettlementCodes(settlementCodes));
    }

    /**
     * 根据合作方编码获取合作方详细信息
     */
    @PostMapping("/getRecordByPartnerCode")
    public AjaxResult getRecordByPartnerCode(@RequestBody String partnerCode){
        return success(settlementRecordService.selectRecordInfoByPartnerCode(partnerCode));
    }





    @PostMapping("/confirm")
    public AjaxResult confirmSettlement(@RequestParam String settlementRecordCode) {
        try {
            settlementRecordService.confirmSettlement(settlementRecordCode);
            return AjaxResult.success("结算单确认成功");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/complete")
    public AjaxResult completeSettlement(@RequestParam String settlementRecordCode) {
        try {
            settlementRecordService.completeSettlement(settlementRecordCode);
            return AjaxResult.success("结算单完成成功");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }


    @PostMapping("/reject")
    public AjaxResult rejectSettlement(@RequestParam String  settlementRecordCode, @RequestParam String rejectReason) {
        try {
            settlementRecordService.rejectSettlement(settlementRecordCode, rejectReason);
            return AjaxResult.success("结算单驳回成功");
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }




}
